package com.yarolegovich.sample;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.RecycleItemProvider;
import ohos.agp.components.Text;
import ohos.app.Context;

import java.util.List;

/**
 * Created by yarolegovich on 17.04.2016.
 */
public class DonationAdapter extends RecycleItemProvider {
    private Context context;
    private List<DonationOption> dataList;

    public DonationAdapter(Context context, List<DonationOption> donationOptions) {
        this.context = context;
        dataList = donationOptions;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public DonationOption getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
        ViewHolder vh;
        if (convertView == null) {
            convertView = LayoutScatter.getInstance(context).parse(ResourceTable.Layout_item_donate_option, componentContainer, false);
            vh = new ViewHolder(convertView);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }
        DonationOption option = getItem(position);
        if (option != null) {
            vh.description.setText(option.description);
            vh.amount.setText(option.amount);
        }
        return convertView;
    }

    private static final class ViewHolder {
        Text description;
        Text amount;
        public ViewHolder(Component component) {
            description = (Text) component.findComponentById(ResourceTable.Id_description);
            amount = (Text) component.findComponentById(ResourceTable.Id_amount);
        }
    }
}
